package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.internetofthings.firefightingiot;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.ConfigKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.internetofthings.FireFightingIOTJavaKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: YangYu
 * @ClassName: IPBroadCastService
 * @Date: Created in 9:31 2022/3/28
 * @Description: IP广播（沃云）
 */
@Service
@Slf4j
public class IPBroadCastService extends BaseService {

    HttpResponse httpResponse;
    /**
     * 上传音乐到中转服务器
     * @param
     * @return
     */
    public RespEntity uploadMusic(MultipartFile file, String homeId) throws IOException {

        String ip = "";
        String methodPath = "";
        ip = ConfigKit.getStr("fireFightingiotJava","config");
        methodPath = FireFightingIOTJavaKit.getStr("uploadMusic","IPBroadcast");
        String domain = ip + methodPath;
        String xml = "";
        httpResponse = HttpRequest.post(domain)
                .form("file",file.getBytes(),file.getOriginalFilename())
                .form("homeId",homeId)
                .execute();
        xml = httpResponse.body();
        JSONObject jsonObject = JSONUtil.parseObj(xml);
        if(jsonObject.getInt("code")==1){
            return RespEntity.fail(jsonObject.getStr("msg"));
        }
        return RespEntity.success(jsonObject.getJSONObject("data"));
    }

    /**
     * 删除音乐文件
     * @param
     * @return
     */
    public RespEntity deleteMusic(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return fireFightingiotJava(jsonParam,"deleteMusic","IPBroadcast");
    }
    /**
     * 获取音乐文件列表
     * @param
     * @return
     */
    public RespEntity getMusicFileList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return fireFightingiotJava(jsonParam,"getMusicFileList","IPBroadcast");
    }

    /**
     * 批量播放音乐
     * @param
     * @return
     */
    public RespEntity playMusicByList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return fireFightingiotJava(jsonParam,"playMusicByList","IPBroadcast");
    }
    /**
     * 批量暂停播放
     * @param
     * @return
     */
    public RespEntity stopMusic(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        return fireFightingiotJava(jsonParam,"stopMusic","IPBroadcast");
    }

    /**
     * 批量播放TTS
     * @param
     * @return
     */
    public RespEntity ttsPlayMusic(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"ttsPlayMusic","IPBroadcast");
    }

    /**
     * 监听
     * @param
     * @return
     */
    public RespEntity monitor(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"monitor","IPBroadcast");
    }

    /**
     * 停止监听
     * @param
     * @return
     */
    public RespEntity stopMonitor(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"stopMonitor","IPBroadcast");
    }

    /**
     * 调节音量
     * @param
     * @return
     */
    public RespEntity setvol(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"setvol","IPBroadcast");
    }
    /**
     * 获取设备信息
     * @param
     * @return
     */
    public RespEntity getDevicesInfo(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"getDevicesInfo","IPBroadcast");
    }
}
